package com.excel.service.excel.v1;



import com.corpgovernment.api.travelstandard.vo.MbTravelstandHotelQuarterVo;
import com.corpgovernment.common.common.CorpBusinessException;
import com.corpgovernment.common.utils.ObjectStringTrimUtils;
import com.corpgovernment.organization.constant.OrganizationResponseCodeEnum;
import com.corpgovernment.organization.util.ExcelUtils;
import com.corpgovernment.travelstandard.vo.HotelQuarterColumnVo;
import com.corpgovernment.travelstandard.vo.HotelQuarterUploadExcelInfo;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.excel.model.ExcelImportConfig;
import com.excel.service.excel.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * 酒店淡旺季地区--暂时不动
 *
 * @author hc
 */
@Service
@Slf4j
public class HotelQuarterProcessorV1<T> implements ExcelImportProcessor<HotelQuarterUploadExcelInfo.HotelQuarterUploadDetailBo,HotelQuarterUploadExcelInfo.HotelQuarterUploadDetailBo, BizImportTask,T> {

    @Autowired
    private ExcelUtils excelUtils;


    @Override
    public List<HotelQuarterUploadExcelInfo.HotelQuarterUploadDetailBo> assembleListFromExcel(BizImportTask task) {
        return null;
    }

    @Override
    public BizImportResult<HotelQuarterUploadExcelInfo.HotelQuarterUploadDetailBo,HotelQuarterUploadExcelInfo.HotelQuarterUploadDetailBo> assembleListFromExcel(BizImportTask task, MultipartFile file){

        BizImportResultImpl<HotelQuarterUploadExcelInfo.HotelQuarterUploadDetailBo,HotelQuarterUploadExcelInfo.HotelQuarterUploadDetailBo> result = new BizImportResultImpl<>();

        List<HotelQuarterUploadExcelInfo.HotelQuarterUploadDetailBo> errorList = new ArrayList<>();
        List<HotelQuarterUploadExcelInfo.HotelQuarterUploadDetailBo> orgList = new ArrayList<>();

        XSSFWorkbook workBook = null;
        try {
            workBook = new XSSFWorkbook(file.getInputStream());
        } catch (IOException e) {
            log.error("上传异常", e);
            throw new CorpBusinessException(OrganizationResponseCodeEnum.FILE_WRONG_FORMAT);
        }

        Sheet sheet = workBook.getSheetAt(0);
        // Excel内容
        List<HotelQuarterColumnVo> beanListFromExcel =
                excelUtils.getBeanListFromExcelTwo(sheet, HotelQuarterColumnVo.class);
        if (CollectionUtils.isEmpty(beanListFromExcel)) {
            throw new CorpBusinessException(OrganizationResponseCodeEnum.HOTEL_TRAVEL_QUARTER_EMPTY);
        }

        beanListFromExcel.forEach(t ->  ObjectStringTrimUtils.allFieldRemoveWarp(t));

        result.setErrorList(errorList);
        result.setDataList(orgList);
        return result;
    }


    @Override
    public BizImportResult<HotelQuarterUploadExcelInfo.HotelQuarterUploadDetailBo,HotelQuarterUploadExcelInfo.HotelQuarterUploadDetailBo> checkExcelData(List<HotelQuarterUploadExcelInfo.HotelQuarterUploadDetailBo> list) {

        BizImportResultImpl<HotelQuarterUploadExcelInfo.HotelQuarterUploadDetailBo,HotelQuarterUploadExcelInfo.HotelQuarterUploadDetailBo> result = new BizImportResultImpl<>();

        List<HotelQuarterUploadExcelInfo.HotelQuarterUploadDetailBo> errorList = new ArrayList<>();
        List<HotelQuarterUploadExcelInfo.HotelQuarterUploadDetailBo> dataList = new ArrayList<>();

        // 通过qlexpress表达式校验数据内容
        for (HotelQuarterUploadExcelInfo.HotelQuarterUploadDetailBo item : list) {

            dataList.add(item);
        }
        result.setErrorList(errorList);
        result.setDataList(dataList);
        return result;
    }


    @Override
    public void writeErrorLog(List<HotelQuarterUploadExcelInfo.HotelQuarterUploadDetailBo> mbImportExcelRecordDetailLogs, BizImportTask task) {

    }


    @Override
    public String getTag() {
        return "hotel_quarter_v1";
    }

    @Override
    public ExcelImportConfig getConfig() {
        ExcelImportConfig config = new ExcelImportConfig();
        config.setIsSharding(true);
        config.setIgnoreHeader(true);
        config.setRollBackType(1);
        return config;
    }

}
